package servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import entity.Product_Info;
import entity.User;
import service.OrderCartService;
import service.ProductInfoService;
import service.UserService;
import service.WarehouseProductService;
import service.impl.OrderCartServiceImpl;
import service.impl.ProductInfoServiceImpl;
import service.impl.UserServiceImpl;
import service.impl.WarehouseProductServiceImpl;

public class EstimateCurrentAmountServlet extends HttpServlet {
	private WarehouseProductService wps = new WarehouseProductServiceImpl();
	private ProductInfoService pis = new ProductInfoServiceImpl();
	private OrderCartService ocs = new OrderCartServiceImpl();
	private UserService us = new UserServiceImpl();
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=utf-8");
		//获取前端传递过来的数据
		String pName = request.getParameter("pName");
		String amount = request.getParameter("amount");
		String mId = request.getParameter("mId");
		String uname = request.getParameter("uname");
		User user = us.queryUserByName(uname);
		//查询出当前的商品
		Product_Info pi = pis.queryProduct_InfoByName(pName);
		//查询当前商品的库存id
		boolean b = false;
		Integer wp_id = wps.queryWarehouseProductIdByProductIdAndModelId(pi.getProduct_id(), Integer.parseInt(mId));
		if(wp_id!=null){
			//查询当前商品的库存是否足够
			 b = wps.productIsEnoughByIdAndNeed(wp_id,Integer.parseInt(amount));
			//如果商品足够的话，传递到前端之前将购物车信息进行更新
			if(b){
				int i = ocs.updateOrderCartByCurrentNeed(pi.getProduct_id(), Integer.parseInt(mId), user.getUserId(), Integer.parseInt(amount));
			}
		}
		//将结果写出到前端
		response.getWriter().print(b);

	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
